package
{
	import com.ascainiao3d.display.Cube3D;
	import com.ascainiao3d.display.World3D;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class MutilCubeTest extends Sprite
	{
		private var cube_list:Vector.<Cube3D>=new Vector.<Cube3D>();
		private var world:World3D;
		private var _vpx:Number;
		private var _vpy:Number;
		private var clickX:Number;
		private var clickY:Number;
		public function MutilCubeTest()
		{
			super();
			_vpx=stage.stageWidth / 2;
			_vpy=stage.stageHeight / 2;
			
			world=new World3D(_vpx,_vpy);
			addChild(world);
			
			for(var i:uint=0;i<3;i++){
				for(var j:uint=0;j<3;j++){
					var cube:Cube3D=new Cube3D(100,0xffffff*Math.random());
					cube.setPosition(-100+100*j,-100+100*i,0);
					world.addObject3D(cube);
					cube_list.push(cube);
				}
			}
			cube_list[0].maskRight=true;
			cube_list[0].maskBottom=true;
			cube_list[1].maskLeft=true;
			cube_list[1].maskRight=true;
			cube_list[1].maskBottom=true;
			cube_list[2].maskLeft=true;
			cube_list[2].maskBottom=true;
			cube_list[3].maskTop=true;
			cube_list[3].maskRight=true;
			cube_list[3].maskBottom=true;
			cube_list[4].maskTop=true;
			cube_list[4].maskLeft=true;
			cube_list[4].maskRight=true;
			cube_list[4].maskBottom=true;
			cube_list[5].maskTop=true;
			cube_list[5].maskLeft=true;
			cube_list[5].maskBottom=true;
			cube_list[6].maskTop=true;
			cube_list[6].maskRight=true;
			cube_list[7].maskLeft=true;
			cube_list[7].maskRight=true;
			cube_list[7].maskTop=true;
			cube_list[8].maskLeft=true;
			cube_list[8].maskTop=true;
			
			world.rendering();
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN,onStageDown);
			stage.addEventListener(MouseEvent.MOUSE_UP,onStageUp);
		}
		
		protected function onStageUp(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected function onStageDown(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			for(var i:uint=0;i<cube_list.length;i++){
				cube_list[i].snapshot();
			}
			clickX=mouseX;
			clickY=mouseY;
			//trace(cube0.z,cube1.z,cube2.z);
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			// TODO Auto-generated method stub
			var angleX:Number = (mouseY - clickY) * .3;
			var angleY:Number = (mouseX - clickX) * .3;
			for(var i:uint=0;i<cube_list.length;i++){
				cube_list[i].rotationXY(angleX,angleY);
			}
			world.rendering();
		}
		
	}
}